<?
require_once('functions.php');

function GetDisorders()
{
	$connect = GetConnection();
	return $connect->query('SELECT * FROM Psi_Disorders');
}
function GetDisorder($id)
{
	$connect = GetConnection();
	$sql = "SELECT * FROM Psi_Disorders WHERE id=$id";
	$results = $connect->query($sql);
	//echo $sql; die();
	$row = $results->fetch_assoc();
	$connect->close();
	return $row;
}
function BlankDisorder()
{
	return array('Name'=>null,'Description'=>null,'DiagnosticCriteria'=>null,'Statistics'=>null,'MiniCase'=>null,'MCDiagnostics'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function SaveDisorder($row)
{
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	$sql = 	"UPDATE Psi_Disorders "
		.	"Set "
		
		.	"Name='$escape[Name]', "
		.	"Description='$escape[Description]', "
		.	"DiagnosticCriteria='$escape[DiagnosticCriteria]', "
		.	"Statistics='$escape[Statistics]', "
		.	"MiniCase='$escape[MiniCase]', "
		.	"MCDiagnostics='$escape[MCDiagnostics]' "
		
		.	"WHERE id=$row[id] ";
	$results = $connect->query($sql);
	// echo $sql; die();
	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function CreateDisorder($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	
	$Name = $_POST['Name'];
	$Description = $_POST['Description'];
	$DiagnosticCriteria = $_POST['DiagnosticCriteria'];
	$Statistics = $_POST['Statistics'];
	$MiniCase = $_POST['MiniCase'];
	$MCDiagnostics = $_POST['MCDiagnostics'];
		
	$sql = 
		"INSERT INTO Psi_Disorders (
			Name, Description, DiagnosticCriteria, Statistics, MiniCase, MCDiagnostics, created_at
		) VALUES (
			 '$escape[Name]', '$escape[Description]', '$escape[DiagnosticCriteria]', '$escape[Statistics]', '$escape[MiniCase]', '$escape[MCDiagnostics]', NOW()
		)";
	
	$result = $connect->query($sql);
	if ($result) {
		// Success!
		header("Location: index.php");
		exit;
	} else {
		// Display error message.
		echo "<p class=\"error\">Subject creation failed.</p>";
		echo "<p class=\"error\">" . mysql_error() . "</p>";
	}

	$connect->close();
}
function DeleteDisorder($id)
{
	$conn = GetConnection();
	$sql = 	"DELETE FROM Psi_Disorders WHERE id=$id";
	$results = $conn->query($sql);
	//echo $sql;
	$error = $conn->error;
	$conn->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

/*function DeleteDisorder($row) {
	$connect = GetConnection();

	if (intval($_GET['id']) == 0) {
			header("Location: index.php");
			die();
	}
	
	$id = mysql_prep($_GET['id']);
	
	if ($subject = GetDisorder($id)) {
		
		$sql = "DELETE FROM Psi_Disorders WHERE id = {$id} LIMIT 1";
		$result = $connect->query($sql);
		if ( $connect->affected_rows == 1 ) {
			header("Location: index.php");
			die();
		} else {
			// Deletion Failed
			echo "<p class=\"error\">Subject deletion failed.</p>";
			echo "<p class=\"error\">" . mysql_error() . "</p>";
			echo "<a href=\"index.php\">Return to Main Page</a>";
		}
	} else {
		// Theorist didn't exist in database
			header("Location: index.php");
			die();
	}

	$connect->close();
}*/
function ValidateDisorder($row)
{
	$errors = array();
	if(empty($row['Name'])) $errors['Name'] = 'Name is required';
	if(empty($row['Description'])) $errors['Description'] = 'Description is required';
	//if(empty($row['DiagnosticCriteria'])) $errors['DiagnosticCriteria'] = 'Abstract Year is required';
	//if(empty($row['Statistics'])) $errors['Statistics'] = 'More Details Required';
	//if(empty($row['MiniCase'])) $errors['MiniCase'] = 'If unknown put N/A';
	//if(empty($row['MCDiagnostics'])) $errors['MCDiagnostics'] = 'If unknown put N/A';

	return count($errors) == 0 ? true : $errors;
}